<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>websocket</title>
</head>

<body>
  <h3>你好Ws</h3>

  <div>
    <input type="text" onkeyup="sendMsg(event)">
  </div>

  <script src="./js/socket.io.min.js"></script>
  <script>
    // 连接服务器 / 作用域
    const client = io.connect('/')

    client.emit('uname','张三')

    // 接受服务器端发送过来的数据
    client.on('message', msg => {
      console.log(msg)
      // 只给请求的用户推送消息
      // client.send('你好服务器')
    })

    function sendMsg(evt) {
      if (evt.keyCode == 13) {
        let value = evt.target.value.trim()
        // send它是 emit('message',消息)的封装方法
        // client.send(value)
        client.emit('message', value)
      }
    }


  </script>
</body>

</html>